package xpn.platform.modules.sys.user;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.web.bind.annotation.*;
import xpn.platform.common.config.XpnPlatformConfig;
import xpn.platform.common.repository.XpnRepositoryController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户 Controller
 *
 * @author duwei
 */
@RepositoryRestController
@ResponseBody
@RequestMapping("users")
public class SysUserController extends XpnRepositoryController<SysUser, Long> {

    @SuppressWarnings("rawtypes")
    @Autowired
    private PagedResourcesAssembler pagedResourcesAssembler;

    @Autowired
    SysUserService userService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    XpnPlatformConfig xpnPlatformConfig;

    @Value("${safeIps}")
    private String safeIps;//4a更新及查询接口白名单IP


    /*
     * 根据指定的角色条件和ou条件查找选人发送的用户列表。
     *
     * @see SysUserService
     */
    @SuppressWarnings("unchecked")
    @GetMapping("search/sendeeselect")
    public Object findSendeeSelectOrgs(@RequestParam(value = "roleCondition", required = false) String roleCondition,
                                       @RequestParam(value = "ouLevel", required = false) String paramOuLevel,
                                       @RequestParam(value = "ouType", required = false) String paramOuType,
                                       @RequestParam(value = "orgId", required = false) String orgId,
                                       @RequestParam(value = "starterId", required = false) String starterId,
                                       @RequestParam(value = "senderId", required = false) String mySenderId,
                                       @RequestParam(value = "specificReceiverUrl", required = false) String specificReceiverUrl, HttpServletRequest request,
                                       PersistentEntityResourceAssembler resourceAssembler) {

        // 参数转换
        Integer ouLevel = -1;
        if (!Strings.isNullOrEmpty(paramOuLevel)) {
            ouLevel = Integer.parseInt(paramOuLevel);
        }

        Integer ouType = -1;
        if (!Strings.isNullOrEmpty(paramOuType)) {
            ouType = Integer.parseInt(paramOuType);
        }
        String xpnTokenName = xpnPlatformConfig.getSecurity().getAuthenticationTokenName();
        String token = request.getHeader(xpnTokenName);
        // 调用服务获取用户列表
        List<SysUser> userList = userService.findSendeeSelectUsers(roleCondition, ouLevel, ouType, orgId,
                starterId, mySenderId, specificReceiverUrl, xpnTokenName, token);
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "o.id"));
        Sort sort = Sort.by(orders);
        Page<SysUser> page = new PageImpl<SysUser>(userList, PageRequest.of(0, 1000,sort), userList.size());
        return pagedResourcesAssembler.toModel(page, resourceAssembler);
    }

    @PatchMapping("{userId}/password")
    public void updatePassword(@PathVariable("userId") String userId,@RequestBody ChangePasswordDTO passwordDTO) {
        userService.updatePassword(userId, passwordDTO.getPassword(), passwordDTO.getNewPassword());
    }

}
